package com.fz.aop;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fz.entity.Log;
import com.fz.entity.Orders;
import com.fz.entity.User;
import com.fz.service.LogService;
import com.fz.service.OrdersService;
import com.fz.service.UserService;
import com.fz.service.UserService;
import com.fz.util.FzUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
@Aspect
public class FzAop {
    @Resource
    private LogService logService;
    @Resource
    private OrdersService orderService;
    @Resource
    private UserService userService;

    @AfterReturning(value = "execution(public * com.fz.service.impl.UserServiceImpl.transfer(..))")
    public void logTransfer(JoinPoint joinPoint) {
        Object[] args=joinPoint.getArgs();
        Long id=(Long)args[0];
        User user1=userService.getById(id);
        BigDecimal money=(BigDecimal)args[2];


        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", (String)args[1]);
        User user = userService.getOne(queryWrapper);
        String msg2=user1.getUsername()+"转入"+money+"元到你的账户";
        String msg="向"+user.getUsername()+"转帐"+money+"元";
        Log log=new Log(FzUtil.randomOrderCode("TF"),msg,"转账",money,"已完成", LocalDateTime.now(),id);
        Log log2=new Log(FzUtil.randomOrderCode("TF"),msg2,"转账",money,"已完成", LocalDateTime.now(),user.getId());
        logService.save(log);
        logService.save(log2);
    }

    @AfterReturning(value = "execution(public * com.fz.service.impl.UserServiceImpl.recharge(..))")
    public void logRecharge(JoinPoint joinPoint) {
        Object[] args=joinPoint.getArgs();
        Long id=(Long)args[0];
        BigDecimal money=(BigDecimal)args[1];
        String msg="充值了"+money+"元";
        Log log=new Log(FzUtil.randomOrderCode("RC"),msg,"充值",money,"已完成", LocalDateTime.now(),id);
        logService.save(log);
    }
    @AfterReturning(value = "execution(public * com.fz.service.impl.OrdersServiceImpl.payTheOrder(..))")
    public void logPayTheOrder(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Long id = (Long)args[1];
        Orders order= orderService.getById(id);
        String msg = "支付了" + order.getTotalAmount() + "元";
        Log log = new Log(order.getOrderId().toString() ,msg, "商品支付", order.getTotalAmount(), "已完成", LocalDateTime.now(), (Long) args[2]);
        logService.save(log);
    }

    @AfterReturning(value = "execution(public * com.fz.service.impl.UserServiceImpl.Buslogin(..))")
    public void logValidateLogin(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String phone = (String) args[0];
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        Log log = new Log(FzUtil.randomOrderCode("LG"), phone , "登录管理端", new BigDecimal(0), "", LocalDateTime.now(), 2L);
        logService.save(log);
    }

    @AfterReturning(value = "execution(public * com.fz.service.impl.UserServiceImpl.login(..))")
    public void logLogin(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String phone = (String) args[0];
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        System.out.println("进入日志中");
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        Log log = new Log(FzUtil.randomOrderCode("LG"), phone , "登录用户端", new BigDecimal(0), "", LocalDateTime.now(), 1L);
        logService.save(log);
    }
    @AfterReturning(value = "execution(public * com.fz.service.impl.UserServiceImpl.payif(..))")
    public void logPayif(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Long id=(Long)args[0];
        BigDecimal money=(BigDecimal)args[1];
        String msg="物业费交了"+money+"元";
        Log log=new Log(FzUtil.randomOrderCode("SF"),msg,"交物业费",money,"已完成",LocalDateTime.now(),id);
        logService.save(log);
    }
}
